<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item prop="searchValue">
        <el-input
          style="width: 300px"
          v-model="queryParams.searchValue"
          placeholder="搜索文章标题或者id"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="shareFlag">
        <el-select v-model="queryParams.shareFlag" placeholder="全部类型" style="width: 117px">
          <el-option label="已开启共享" value="1"></el-option>
          <el-option label="已关闭共享" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="city">
        <el-select v-model="queryParams.city" placeholder="选择城市" style="width: 103px" @change="getCountyOptions">
          <el-option
            v-for="item in cityOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="county">
        <el-select v-model="queryParams.county" placeholder="全部区县" style="width: 103px">
          <el-option
            v-for="item in countyOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery" round style="width: 90px">查询</el-button>
        <el-button type="primary" icon="el-icon-refresh" round @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="sharemanuscriptsList" @selection-change="handleSelectionChange" stripe>
      <el-table-column label="稿件ID" align="center" prop="articleId" />
      <el-table-column label="文章标题" align="center" prop="title" width="600px">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="共享时间" align="center" prop="createTime" />
      <el-table-column label="来源" align="center" prop="sourceFromName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="showMaterialDialog(scope.row.articleId)"
          >查看素材</el-button>
          <el-button
            type="text"
            @click="handleSync(scope.row.articleId)"
          >同步</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--    查看素材对话框-->
    <el-dialog
      title="查看素材"
      :visible.sync="materialDialogVisible"
      width="70%"
      @close="resetTab">
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="图片" name="img">
          <div v-if="imgsTotal>0" style="padding-left: 22px;height: 352px;overflow: auto">
            <el-image
              v-for="img in imgs"
              :key="img.materialName"
              style="width: 240px; height: 160px;margin: 0 13px 13px 0"
              :src="img.materialUrl">
            </el-image>
          </div>
          <div v-else style="padding-left: 22px;height: 352px;overflow: auto;text-align: center;font-size: 16px">暂无数据</div>
          <pagination
            v-show="imgsTotal>0"
            :total="imgsTotal"
            :page.sync="imgQueryParams.pageNum"
            :limit.sync="imgQueryParams.pageSize"
            @pagination="getMaterialList('img')"
            style="padding: 0 !important;"
          />
        </el-tab-pane>
        <el-tab-pane label="视频" name="video">
          <div v-if="videosTotal>0" style="padding-left: 22px;height: 352px;overflow: auto;">
            <video
              v-for="video in videos"
              :key="video.materialName"
              :src="video.materialUrl"
              controls="controls"
              style="width: 240px; height: 160px;margin: 0 13px 13px 0;object-fit: fill;">
            </video>
          </div>
          <div v-else style="padding-left: 22px;height: 352px;overflow: auto;text-align: center;font-size: 16px">暂无数据</div>
          <pagination
            v-show="videosTotal>0"
            :total="videosTotal"
            :page.sync="videoQueryParams.pageNum"
            :limit.sync="videoQueryParams.pageSize"
            @pagination="getMaterialList('video')"
            style="padding: 0 !important;"
          />
        </el-tab-pane>
        <el-tab-pane label="音频" name="audio">
          <div v-if="audiosTotal>0" style="padding-left: 22px;height: 352px;overflow: auto;">
            <audio
              v-for="audio in audios"
              :key="audio.materialName"
              :src="audio.materialUrl"
              controls="controls"
              style="width: 240px; height: 54px;margin: 0 13px 13px 0;">
            </audio>
          </div>
          <div v-else style="padding-left: 22px;height: 352px;overflow: auto;text-align: center;font-size: 16px;">
            暂无数据
          </div>
          <pagination
            v-show="audiosTotal>0"
            :total="audiosTotal"
            :page.sync="audioQueryParams.pageNum"
            :limit.sync="audioQueryParams.pageSize"
            @pagination="getMaterialList('audio')"
            style="padding: 0 !important;"
          />
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="materialDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="materialDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAllAreaData } from "@/api/manuscripts/source";
import { listSharematerial,listManuscriptMaterial,syncMaterial } from "@/api/manuscripts/sharematerial";

export default {
  name: "Sharematerial",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 共享稿件管理表格数据
      sharemanuscriptsList: [],
      // 城市和区县选项数据
      areasData: [],
      // 城市下拉选项
      cityOptions: [],
      // 区县下拉选项
      countyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        shareFlag: undefined,
        city: undefined,
        county: undefined,
        sourceFrom: undefined,
      },
      // 查看素材对话框
      materialDialogVisible: false,
      activeName: 'img',
      imgs: [],
      imgQueryParams: {
        pageNum: 1,
        pageSize: 10,
        type: "img",
        articleId: ''
      },
      imgsTotal: 0,
      videos: [],
      videoQueryParams: {
        pageNum: 1,
        pageSize: 10,
        type: "video",
        articleId: '',
      },
      videosTotal: 0,
      audios: [],
      audioQueryParams: {
        pageNum: 1,
        pageSize: 10,
        type: "audio",
        articleId: ''
      },
      audiosTotal: 0
    };
  },
  created() {
    this.getList();
    this.getAllAreas();
  },
  methods: {
    /** 查询共享稿件管理列表 */
    getList() {
      this.loading = true;
      // 将city,county参数合并为sourceFrom
      this.queryParams.sourceFrom = this.queryParams.county || this.queryParams.city;
      listSharematerial(this.queryParams).then(response => {
        this.sharemanuscriptsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 查询城市和区县选项数据
    getAllAreas() {
      getAllAreaData().then(response => {
        // {
        //   "510100-成都市": ["510100-全部区县", "510104-锦江区"],
        //   "510300-自贡市": ["510300-全部区县", "510302-自流井区"],
        //   ...
        // }
        this.areasData = response.data;
        // 构造城市选项数据
        Object.keys(this.areasData).forEach((item) => {
          this.cityOptions.push({
            value: item.split("-")[0],
            label: item.split("-")[1],
          })
        })
      })
    },

    // 城市change时获取对应的区县选项数据
    getCountyOptions(val) {
      let cityVals = [];
      this.countyOptions = [];
      this.queryParams.county = undefined;
      Object.keys(this.areasData).forEach((item) => {
        cityVals.push(item.split("-")[0]);
      });
      let cityIndex = cityVals.indexOf(val);
      let cityKey = this.cityOptions[cityIndex].value + "-" + this.cityOptions[cityIndex].label;
      this.areasData[cityKey].forEach((item) => {
        this.countyOptions.push({
          value: item.split("-")[0],
          label: item.split("-")[1],
        })
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    // 显示查看素材对话框
    showMaterialDialog(articleId) {
      this.imgQueryParams.articleId = articleId;
      this.videoQueryParams.articleId = articleId;
      this.audioQueryParams.articleId = articleId;
      this.materialDialogVisible = true;
      this.getMaterialList('img');
    },

    // 查询稿件图片、视频、音频素材列表
    getMaterialList(type) {
      listManuscriptMaterial(this[`${type}QueryParams`]).then(response => {
        this[`${type}s`] = response.rows;
        this[`${type}sTotal`] = response.total;
      });
    },

    // tab 被选中时触发
    handleTabClick(tab) {
      this.getMaterialList(tab.name);
    },

    // 关闭弹窗后重置tab
    resetTab() {
      this.activeName = "img";
      this.imgQueryParams.pageNum = 1;
      this.imgQueryParams.pageSize = 10;
      this.videoQueryParams.pageNum = 1;
      this.videoQueryParams.pageSize = 10;
      this.audioQueryParams.pageNum = 1;
      this.audioQueryParams.pageSize = 10;
    },

    // 同步素材
    handleSync(articleId) {
      syncMaterial({ articleId }).then(response => {
        if (response.code === 200) {
          this.msgSuccess("同步成功");
          this.getList();
        } else {
          this.msgError(response.msg);
        }
      });
    }
  }
};
</script>
